/**
 * 
 */
package ads.engenharia.dal;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ads.engenharia.entity.Discipline;
import ads.engenharia.entity.Schedule;

/**
 * @author Lanedraex
 *
 */
public class DisciplineScheduleDAO {

	private String sql;
	private String tableName = "Discipline_Schedule";

	private EntityManager em = new EntityManager() {

		@Override
		public Object treatResultSet(ResultSet resultSet) throws SQLException {
			Discipline discipline = new Discipline();

			discipline.schedule(searchDisciplineSchedule(discipline.id()));
			return discipline;
		}
	};

	/**
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void save(Discipline discipline) throws SQLException {

		sql = " insert into " + tableName
				+ " ( discipline_id, day, time, hour ) values ( ?, ?, ?, ?) ";

		for (Schedule.Day day : discipline.schedule().Days) {

			for (Schedule.Time time : discipline.schedule().Time) {

				for (Schedule.Hour hour : discipline.schedule().Hours) {

					em.executeQuery(sql, discipline.id(), day, time, hour);
				}
			}
		}
	}

	/**
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void update(Discipline discipline) throws SQLException {

		sql = "update";

		for (Schedule.Day day : discipline.schedule().Days) {

			for (Schedule.Time time : discipline.schedule().Time) {

				for (Schedule.Hour hour : discipline.schedule().Hours) {

					em.executeQuery(sql, discipline.id(), day, time, hour);
				}
			}
		}
	}

	/**
	 * 
	 * @param discipline
	 * @return completedSchedule
	 * @throws SQLException
	 */
	public Schedule searchDisciplineSchedule(Discipline discipline)
			throws SQLException {

		sql = " select distinct day, time, hour from " + tableName
				+ " where discipline_id = ? order by day, time, hour ";

		@SuppressWarnings("unchecked")
		ArrayList<Schedule> schedules = (ArrayList<Schedule>) em.getResultList(
				sql, discipline.id());

		Schedule completedSchedule = new Schedule();

		// Adiciona cada parte do array de Schedule num schedule final
		for (Schedule schedule : schedules) {
			completedSchedule.Days.add(schedule.Days.get(0));
			completedSchedule.Time.add(schedule.Time.get(0));
			completedSchedule.Hours.add(schedule.Hours.get(0));
		}

		return completedSchedule;
	}

	/**
	 * 
	 * @param id
	 * @return completedSchedule
	 * @throws SQLException
	 */
	public Schedule searchDisciplineSchedule(int id) throws SQLException {

		sql = " select distinct day, time, hour from " + tableName
				+ " where discipline_id = ? order by day, time, hour ";

		@SuppressWarnings("unchecked")
		ArrayList<Schedule> schedules = (ArrayList<Schedule>) em.getResultList(
				sql, id);

		Schedule completedSchedule = new Schedule();

		// Adiciona cada parte do array de Schedule num schedule final
		for (Schedule schedule : schedules) {
			completedSchedule.Days.add(schedule.Days.get(0));
			completedSchedule.Time.add(schedule.Time.get(0));
			completedSchedule.Hours.add(schedule.Hours.get(0));
		}

		return completedSchedule;
	}
}
